//
//  SY_mainView.m
//  
//
//  Created by 张铁军 on 16/8/16.
//
//

#import "SY_mainView.h"
#import "SY_collecTionCell.h"
#import "SY_headerSectioncellView.h"
#import "ShouYeModel.h"

@implementation SY_mainView{
    UIView *_tableHeadView;
    RWcollectionView *_myCollectionView;
    NSArray *_fenLeiAry;
    NSArray *_fenLeiImageAry;
    UIScrollView *_btnScrollView;
    int _page;
    NSMutableArray * _productAry;
    NSMutableArray * _array;
}

-(instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        
        [self initView];
        
    }
    return self;
}

-(void)initView{
    _page = 1;
    [self requestLB];
    [self createdSubView];
    
}
-(void)requestLB{
    [NetWorking NEWjieKouQingQiu:SYLB_queryCotElashimg ANDIP:IP And:nil anBlock:^(NSDictionary *dic, NSError *error) {
        if (!error) {
            if ([dic[Code] intValue] == 200) {
                NSArray *arr = dic[Response];
                _array = [[NSMutableArray alloc]init];
                [arr enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    ShouYeModel * model = [ShouYeModel objectFromDictionary:obj];
                    [_array addObject:model];
                  }];
                NSMutableArray  * imageArr = [[NSMutableArray alloc]init];
                [_array enumerateObjectsUsingBlock:^(id  _Nonnull obj, NSUInteger idx, BOOL * _Nonnull stop) {
                    ShouYeModel * a = obj;
                    [imageArr addObject:a.imgUrl];
                }];
                _myTopSdcleScrollView.imageURLStringsGroup = imageArr;
                  [self requestDataWith:1];
            }
        }
    }];
}
-(void)createdSubView{
    //    self.backgroundColor = ZONGCOLOR;
    
    
    _tableHeadView = [[UIView alloc] initWithFrame:CGRectMake(0, 0, KScreenWidth, 170*_kscH +20 * _kscH + 90*_kscH)];
    _tableHeadView.backgroundColor = [UIColor whiteColor];
    //    [self addSubview:_tableHeadView];
    
    _myTopSdcleScrollView = [SDCycleScrollView cycleScrollViewWithFrame:CGRectMake(0, 0, KScreenWidth, 170 * _kscH)imageURLStringsGroup:nil];
    
    _myTopSdcleScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentCenter;
    _myTopSdcleScrollView.delegate = self;
    _myTopSdcleScrollView.autoScrollTimeInterval = 3;
    _myTopSdcleScrollView.tag = 0;
    
    _myTopSdcleScrollView.dotColor = [UIColor whiteColor]; // 自定义分页控件小圆标颜色
    _myTopSdcleScrollView.placeholderImage = [UIImage imageNamed:PLaceHolderImage];
    [_tableHeadView addSubview:_myTopSdcleScrollView];
    
     [self drawScrollView];
    [self contentView];
   
}

-(void)drawScrollView{
    _btnScrollView = [[UIScrollView alloc] initWithFrame:CGRectMake(0, _myTopSdcleScrollView.bottom, KScreenWidth, 105*_kscH)];
    _btnScrollView.backgroundColor = [UIColor whiteColor];
    _btnScrollView.scrollEnabled = YES;
    _btnScrollView.showsHorizontalScrollIndicator = NO;
    [_tableHeadView addSubview:_btnScrollView];
}

-(void)setBiaoJi:(int)biaoJi{
    
    NSArray *buttonImageArr;
    ;
    CGFloat btn_x = 10*_kscW;
    CGFloat btn_jianGe = 10*_kscW;
    CGFloat btn_height = 80*_kscH;
    CGFloat btn_weidth = btn_height;
    
    if (biaoJi == 1) {
          _fenLeiAry = @[@"话费充值",@"积分商城",@"一元购",@"线下商铺",@"公众服务",@"话费充值",@"积分商城",@"一元购",@"线下商铺",@"公众服务"];
        buttonImageArr = @[@""];
        
        for (int i = 0; i < _fenLeiAry.count; i ++) {
            
            UIButton *anNiuBtn = [[UIButton alloc] initWithFrame:CGRectMake(btn_x + i*(btn_jianGe + btn_weidth), 5*_kscH, btn_weidth, btn_height)];
             anNiuBtn.backgroundColor = [UIColor blackColor];
            anNiuBtn.layer.cornerRadius = btn_height/2.0;
            anNiuBtn.clipsToBounds = YES;
            anNiuBtn.tag = i;
            [anNiuBtn addTarget:self.delegate action:@selector(gongNengBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
            [anNiuBtn setImage:[UIImage imageNamed:[_fenLeiAry objectAtIndex:i]] forState:UIControlStateNormal];
            [_btnScrollView addSubview:anNiuBtn];
            UILabel * lable = [[UILabel alloc]initWithFrame:CGRectMake(btn_x + i*(btn_jianGe + btn_weidth),anNiuBtn.bottom + 5*_kscH, btn_weidth, 15)];
            lable.text = _fenLeiAry[i];
            lable.font = [UIFont systemFontOfSize:14];
            lable.textAlignment = NSTextAlignmentCenter;
            lable.textColor = MaintextColor;
            [_btnScrollView addSubview:lable];
            if (i ==( _fenLeiAry.count-1)) {
                _btnScrollView.contentSize = CGSizeMake(anNiuBtn.right + btn_x, 0);
            }

//            UIButton *anNiuBtn = [[UIButton alloc]initWithFrame:CGRectMake(17.5 * _kscW + 80 *_kscW * (i%4), 10 * _kscH + i/4 * 70 * _kscH + _myTopSdcleScrollView.bottom, 45 * _kscW, 45 * _kscH)];
//            anNiuBtn.layer.cornerRadius = 45 * _kscH/2;
//            anNiuBtn.backgroundColor = [UIColor redColor];
//            anNiuBtn.tag = i;
//            [anNiuBtn addTarget:self.delegate action:@selector(gongNengBtnClicked:) forControlEvents:UIControlEventTouchUpInside];
//            [_tableHeadView addSubview:anNiuBtn];
//            
//            BaseLabel *neiRongLAbel = [[BaseLabel alloc]initWithFrame:CGRectMake(anNiuBtn.left - 20 * _kscW, anNiuBtn.bottom + 10 * _kscH, 90 * _kscW, 12 * _kscH)andFont:12];
//            neiRongLAbel.text = [_fenLeiAry objectAtIndex:i];
//            neiRongLAbel.textAlignment = NSTextAlignmentCenter;
//            neiRongLAbel.textColor = MaintextColor;
//            [_tableHeadView addSubview:neiRongLAbel];
        }

    }
}
-(void)requestDataWith:(int)first{
    NSMutableDictionary *requestDic = [[NSMutableDictionary alloc]init];
//    [requestDic setObject:@"14" forKey:@"catId"];
    [requestDic setObject:[NSString stringWithFormat:@"%d",_page] forKey:@"pageNum"];
    [requestDic setObject:@"10" forKey:@"pageSize"];
    [requestDic setObject:@"5" forKey:@"sortType"];
    [requestDic setObject:@"1" forKey:@"isBest"];
    
    [NetWorking NEWjieKouQingQiu:FL_findByCategory ANDIP:IP And:requestDic anBlock:^(NSDictionary *dic, NSError *error) {
         [[NSNotificationCenter defaultCenter]postNotificationName:@"轮播图" object:@"0"];
        [_myCollectionView headerEndRefreshing];
        [_myCollectionView footerEndRefreshing];
        if (!error) {
            if ([[dic objectForKey:Code]intValue] == 200) {
                NSArray *data = dic[Response];
                
                if (first == 1) {
                    _productAry = [[NSMutableArray alloc]init];
                }
                [data enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *  stop) {
                    FL_ChanPinModel *chanPinModel = [FL_ChanPinModel objectFromDictionary:obj];
                    [_productAry addObject:chanPinModel];
                }];
                [_myCollectionView reloadData];
            }
        }
    }];
}
-(void)headRefresh{
    _page = 1;
    [self requestDataWith:1];
}
-(void)footerRefresh{
    _page ++;
    [self requestDataWith:0];
}
-(void)contentView{
    
    UICollectionViewFlowLayout *flow1 = [[UICollectionViewFlowLayout alloc]init];
    flow1.itemSize = CGSizeMake( (KScreenWidth-2) / 2, KScreenWidth/2+ 30);
    
    //    flow1.columsCount=2; //显示2列
//    flow1.sectionInset=UIEdgeInsetsMake(0 , 10 * _kscW, 0, 25 * _kscW);
    flow1.minimumInteritemSpacing = 1 ;
    flow1.minimumLineSpacing = 1;
//    flow1.headerReferenceSize = CGSizeMake(KScreenWidth, 200);

    _myCollectionView = [[RWcollectionView alloc]initWithFrame:CGRectMake(0, 0, KScreenWidth, self.height)collectionViewLayout:flow1];
    [_myCollectionView addHeaderWithTarget:self action:@selector(headRefresh)];
  //  [_myCollectionView headerBeginRefreshing];
    [_myCollectionView addFooterWithTarget:self action:@selector(footerRefresh)];
    _myCollectionView.delegate = self;
    _myCollectionView.dataSource = self;
    [_myCollectionView addSubview:_tableHeadView];
    _myCollectionView.backgroundColor = LIGHTGRAYBACKCOLOR;
    [_myCollectionView registerClass:[SY_collecTionCell class] forCellWithReuseIdentifier:@"SY_collecTionCell"];
    [_myCollectionView registerClass:[SY_headerSectioncellView class] forSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SY_headerSectioncellView"];
    [self addSubview:_myCollectionView];
}

//heardView的高度
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section{
    
    if (section == 0) {
        return  CGSizeMake(KScreenWidth, _tableHeadView.height + 30*_kscH);
    }else{
        return  CGSizeMake(KScreenWidth, 30 * _kscH);
    }
}

- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section{
//    if (section == 0) {
//        return 0;
//    }
    return _productAry.count;
}

-(UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath{
    
    SY_headerSectioncellView *headView;
    
    if([kind isEqual:UICollectionElementKindSectionHeader])
    {
        if(indexPath.section == 0){
            headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SY_headerSectioncellView" forIndexPath:indexPath];
            headView.backView.top = _tableHeadView.height;
            headView.nameLabel.text = @"精品推荐";
        }else{
            headView = [collectionView dequeueReusableSupplementaryViewOfKind:UICollectionElementKindSectionHeader withReuseIdentifier:@"SY_headerSectioncellView" forIndexPath:indexPath];
//            [headView.tenYuanBtn addTarget:self action:@selector(tenBtnClicked) forControlEvents:UIControlEventTouchUpInside];
//            headView.titleLael.text = @"人气夺宝";
        }
        
    }
    
    return headView;
}
-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
    FL_ChanPinModel *chanPin = _productAry[indexPath.row];
  
    [self.delegate tiaoZhuan:chanPin];
  
}

- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath{
    FL_ChanPinModel *chanPin = _productAry[indexPath.row];
    SY_collecTionCell * cell = [collectionView dequeueReusableCellWithReuseIdentifier:@"SY_collecTionCell" forIndexPath:indexPath];
    cell.backgroundColor = [UIColor whiteColor];
    [cell.productImage sd_setImageWithURL:[NSURL URLWithString:[NSString stringWithFormat:@"%@",chanPin.coverImage]]];
    cell.namelabel.text = chanPin.name;
    
    return cell;
}

-(NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView{
    return 1;
}

@end
